package com.example.su_shi_tan_mine.child

import android.view.View
import android.webkit.WebView
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.example.library_common.utils.normalShortToast
import com.example.su_shi_tan_bas_lib.base.LazyFragment
import com.example.su_shi_tan_bas_lib.global.AppGlobals
import com.example.su_shi_tan_bas_lib.networking.RestClient
import com.example.su_shi_tan_mine.R
import com.example.su_shi_tan_mine.arouter.SubPageARouter
import com.example.su_shi_tan_mine.child.bean.MessagePageDetailBean

@Route(path = SubPageARouter.MessagePageDetail)
class MessagePageDetailFragment : LazyFragment() {

    @JvmField
    @Autowired(name = SubPageARouter.MessagePageDetailParam)
    var mId: String = ""

    private val mToolbar by lazy {
        view?.findViewById<Toolbar>(R.id.mToolbar)
    }

    private val titleTv by lazy {
        view?.findViewById<TextView>(R.id.titleTv)
    }

    private val mWebView by lazy {
        view?.findViewById<WebView>(R.id.mWebView)
    }

    override fun lazyInit() {
        var url = AppGlobals.getApplication().getString(R.string.hot_details)
        url += "/$mId"
        RestClient.builder()
            .url(url)
            .success {
                parseData(it)
            }
            .error { _, msg ->
                normalShortToast(_mActivity,msg)
            }.failure {
                normalShortToast(_mActivity,R.string.net_work_failed)
            }.build().get()

    }

    private fun parseData(it: String?) {
        val obj = JSON.parseObject(it, MessagePageDetailBean::class.java)
        titleTv?.text = obj.title
        mWebView?.loadData(obj?.content?:"", "text/html; charset=UTF-8", null)
    }

    override fun initView(view: View) {

        mToolbar?.setNavigationOnClickListener { onBackPressedSupport() }
    }

    override fun getLayoutId(): Any {
        return R.layout.message_page_detail_fragment
    }
}